import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";
var _excluded = ["storageConfigId", "endPoint", "endPointBefore"];

var _dec, _dec2, _dec3, _dec4, _dec5, _dec6, _dec7, _dec8, _dec9, _dec10, _dec11, _dec12, _class, _class2;

/**
 * Storage 文件存储配置页面
 * @date: 2018-7-25


 */
import React from 'react';
import { connect } from 'dva';
import { Form, Radio } from 'antd';
import { Bind } from 'lodash-decorators';
import Lov from 'components/Lov';
import { Content, Header } from 'components/Page';
import { Button as ButtonPermission, FormItem } from 'components/Permission';
import formatterCollections from 'utils/intl/formatterCollections';
import intl from 'utils/intl';
import notification from 'utils/notification';
import { isTenantRoleLevel } from 'utils/utils';
import StorageDrawer from "./StorageDrawer";
import StorageList from "./StorageList";
var RadioGroup = Radio.Group;
var Storage = (_dec = formatterCollections({
  code: 'cfile.storage'
}), _dec2 = Form.create({
  fieldNameProp: null
}), _dec3 = connect(function (_ref) {
  var loading = _ref.loading,
      storage = _ref.storage,
      user = _ref.user;
  return {
    fetchDefaultLoading: loading.effects['storage/fetchDefaultStorage'],
    fetchStorageLoading: loading.effects['storage/fetchStorage'],
    saveLoading: loading.effects['storage/updateStorage'],
    storage: storage,
    user: user
  };
}), _dec4 = Bind(), _dec5 = Bind(), _dec6 = Bind(), _dec7 = Bind(), _dec8 = Bind(), _dec9 = Bind(), _dec10 = Bind(), _dec11 = Bind(), _dec12 = Bind(), _dec(_class = _dec2(_class = _dec3(_class = (_class2 = /*#__PURE__*/function (_React$Component) {
  _inherits(Storage, _React$Component);

  var _super = _createSuper(Storage);

  function Storage() {
    var _this;

    _classCallCheck(this, Storage);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _super.call.apply(_super, [this].concat(args));
    _this.state = {
      editVisible: false,
      editData: {},
      storageType: '' // 当前文件存储类型

    };
    return _this;
  }

  _createClass(Storage, [{
    key: "componentDidMount",
    value: function componentDidMount() {
      var dispatch = this.props.dispatch;
      dispatch({
        type: 'storage/init'
      });
      this.fetchDefaultStorage();
    }
    /**
     * fetchDefaultStorage - 获取默认配置
     */

  }, {
    key: "fetchDefaultStorage",
    value: function fetchDefaultStorage() {
      var _this2 = this;

      var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      var _this$props = this.props,
          dispatch = _this$props.dispatch,
          tenantId = _this$props.storage.tenantId;
      dispatch({
        type: 'storage/fetchDefaultStorage',
        payload: _objectSpread({
          tenantId: tenantId
        }, params)
      }).then(function (res) {
        if (res && res.content) {
          var _ref2 = res.content || [],
              _ref3 = _slicedToArray(_ref2, 1),
              _ref3$ = _ref3[0],
              defaultStorage = _ref3$ === void 0 ? {} : _ref3$;

          var storageType = "".concat(defaultStorage.storageType || '1');

          _this2.setState({
            storageType: storageType
          });
        }
      });
    }
    /**
     * fetchStorage - 获取文件
     * @param {string} params.text - 选择的租户值
     * @param {object} params.record - 选择的租户行数据
     */

  }, {
    key: "fetchStorage",
    value: function fetchStorage() {
      var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      var _this$props2 = this.props,
          dispatch = _this$props2.dispatch,
          tenantId = _this$props2.storage.tenantId;
      var storageType = this.state.storageType;
      dispatch({
        type: 'storage/fetchStorage',
        payload: _objectSpread({
          tenantId: tenantId,
          storageType: storageType
        }, params)
      });
    }
    /**
     * handleChangeOrg - 租户切换
     * @param {string} text - 选择的租户值
     * @param {object} record - 选择的租户行数据
     */

  }, {
    key: "handleChangeOrg",
    value: function handleChangeOrg(text, record) {
      var _this$props3 = this.props,
          dispatch = _this$props3.dispatch,
          form = _this$props3.form;
      form.resetFields();
      dispatch({
        type: 'storage/updateState',
        payload: {
          tenantId: record.tenantId
        }
      });
      this.fetchDefaultStorage({
        tenantId: record.tenantId
      });
    }
    /**
     * handleSelectStorage - 监听不同的存储类型切换
     * @param {object} e - 事件对象
     */

  }, {
    key: "handleSelectStorage",
    value: function handleSelectStorage(e) {
      var _this$props4 = this.props,
          dispatch = _this$props4.dispatch,
          form = _this$props4.form;
      form.resetFields();
      dispatch({
        type: 'storage/updateState',
        payload: {
          storageDataList: []
        }
      });
      this.setState({
        storageType: e.target.value
      });
      this.fetchStorage({
        storageType: e.target.value
      });
    }
  }, {
    key: "handleCancel",
    value: function handleCancel() {
      this.setState({
        editVisible: false,
        editData: {}
      });
    }
  }, {
    key: "handleEdit",
    value: function handleEdit() {
      var record = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      this.setState({
        editVisible: true,
        editData: record
      });
    }
  }, {
    key: "handleCreate",
    value: function handleCreate() {
      this.setState({
        editVisible: true,
        editData: {}
      });
    }
  }, {
    key: "handleDelete",
    value: function handleDelete(record) {
      var _this3 = this;

      var dispatch = this.props.dispatch;
      dispatch({
        type: 'storage/deleteStorage',
        payload: record
      }).then(function (res) {
        if (res) {
          notification.success();

          _this3.fetchStorage({
            storageType: record.storageType
          });
        }
      });
    }
    /**
     * 保存文件服务配置
     */

  }, {
    key: "handleSaveStorage",
    value: function handleSaveStorage(fieldsValue) {
      var _this4 = this;

      var _this$props5 = this.props,
          dispatch = _this$props5.dispatch,
          storage = _this$props5.storage;
      var tenantId = storage.tenantId;

      var storageConfigId = fieldsValue.storageConfigId,
          endPoint = fieldsValue.endPoint,
          endPointBefore = fieldsValue.endPointBefore,
          others = _objectWithoutProperties(fieldsValue, _excluded);

      var params = _objectSpread(_objectSpread({}, others), {}, {
        endPoint: "".concat(others.storageType === '3' ? endPointBefore : '').concat(endPoint),
        storageConfigId: storageConfigId || '',
        accessControl: others.accessControl ? others.accessControl[1] : '',
        tenantId: tenantId
      });

      dispatch({
        type: 'storage/updateStorage',
        payload: params
      }).then(function (res) {
        if (res) {
          notification.success();

          _this4.handleCancel();

          _this4.fetchStorage({
            storageType: fieldsValue.storageType
          });
        }
      });
    }
  }, {
    key: "render",
    value: function render() {
      var _this5 = this;

      var _this$props6 = this.props,
          form = _this$props6.form,
          path = _this$props6.match.path,
          _this$props6$fetchDef = _this$props6.fetchDefaultLoading,
          fetchDefaultLoading = _this$props6$fetchDef === void 0 ? false : _this$props6$fetchDef,
          _this$props6$fetchSto = _this$props6.fetchStorageLoading,
          fetchStorageLoading = _this$props6$fetchSto === void 0 ? false : _this$props6$fetchSto,
          _this$props6$saveLoad = _this$props6.saveLoading,
          saveLoading = _this$props6$saveLoad === void 0 ? false : _this$props6$saveLoad,
          tenantName = _this$props6.user.currentUser.tenantName,
          _this$props6$storage = _this$props6.storage,
          _this$props6$storage$ = _this$props6$storage.storageDataList,
          storageDataList = _this$props6$storage$ === void 0 ? [] : _this$props6$storage$,
          _this$props6$storage$2 = _this$props6$storage.serverProviderList,
          serverProviderList = _this$props6$storage$2 === void 0 ? [] : _this$props6$storage$2,
          _this$props6$storage$3 = _this$props6$storage.prefixStrategyList,
          prefixStrategyList = _this$props6$storage$3 === void 0 ? [] : _this$props6$storage$3,
          _this$props6$storage$4 = _this$props6$storage.microsoftEndpointList,
          microsoftEndpointList = _this$props6$storage$4 === void 0 ? [] : _this$props6$storage$4,
          _this$props6$storage$5 = _this$props6$storage.radioTypeList,
          radioTypeList = _this$props6$storage$5 === void 0 ? [] : _this$props6$storage$5;
      var _this$state = this.state,
          editVisible = _this$state.editVisible,
          _this$state$editData = _this$state.editData,
          editData = _this$state$editData === void 0 ? {} : _this$state$editData,
          storageType = _this$state.storageType;
      var storageListProps = {
        path: path,
        serverProviderList: serverProviderList,
        storageDataList: storageDataList,
        type: storageType,
        loading: fetchDefaultLoading || fetchStorageLoading,
        onEdit: this.handleEdit,
        onDelete: this.handleDelete
      };

      var _ref4 = radioTypeList.find(function (item) {
        return storageType === item.value;
      }) || {},
          _ref4$meaning = _ref4.meaning,
          meaning = _ref4$meaning === void 0 ? '' : _ref4$meaning;

      var StorageDrawerProps = {
        serverProviderList: serverProviderList,
        prefixStrategyList: prefixStrategyList,
        microsoftEndpointList: microsoftEndpointList,
        type: storageType,
        title: editData.storageConfigId !== undefined ? intl.get('cfile.storage.view.title.storageType.edit', {
          type: meaning
        }).d("\u7F16\u8F91".concat(meaning)) : intl.get('cfile.storage.view.title.storageType.create', {
          type: meaning
        }).d("\u65B0\u5EFA".concat(meaning)),
        loading: saveLoading,
        modalVisible: editVisible,
        initData: editData,
        onOk: this.handleSaveStorage,
        onCancel: this.handleCancel
      };
      return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Header, {
        title: intl.get('cfile.storage.view.message.title').d('文件存储配置')
      }, /*#__PURE__*/React.createElement(ButtonPermission, {
        type: "primary",
        icon: "plus",
        permissionList: [{
          code: "".concat(path, ".button.create"),
          type: 'button',
          meaning: '文件存储配置-新建'
        }],
        onClick: this.handleCreate,
        disabled: fetchDefaultLoading
      }, intl.get('cpaas.common.button.create').d('新建')), /*#__PURE__*/React.createElement(ButtonPermission, {
        icon: "sync",
        onClick: this.fetchStorage
      }, intl.get('cpaas.common.button.refresh').d('刷新')), !isTenantRoleLevel() && /*#__PURE__*/React.createElement(Lov, {
        className: "page-head-operation",
        style: {
          width: '200px'
        },
        value: "0",
        allowClear: false,
        textValue: tenantName,
        code: "CPFM.TENANT",
        onChange: function onChange(text, record) {
          _this5.handleChangeOrg(text, record);
        }
      })), /*#__PURE__*/React.createElement(Content, null, /*#__PURE__*/React.createElement(Form, null, /*#__PURE__*/React.createElement(FormItem, {
        label: intl.get('cfile.storage.model.storage.storageType').d('存储类型'),
        labelCol: {
          span: 2
        },
        wrapperCol: {
          span: 20
        }
      }, form.getFieldDecorator('storageType', {
        initialValue: storageType
      })( /*#__PURE__*/React.createElement(RadioGroup, {
        onChange: this.handleSelectStorage
      }, radioTypeList.map(function (item) {
        return /*#__PURE__*/React.createElement(Radio, {
          value: item.value
        }, item.meaning);
      }))))), /*#__PURE__*/React.createElement(StorageList, storageListProps), editVisible && /*#__PURE__*/React.createElement(StorageDrawer, StorageDrawerProps)));
    }
  }]);

  return Storage;
}(React.Component), (_applyDecoratedDescriptor(_class2.prototype, "fetchDefaultStorage", [_dec4], Object.getOwnPropertyDescriptor(_class2.prototype, "fetchDefaultStorage"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "fetchStorage", [_dec5], Object.getOwnPropertyDescriptor(_class2.prototype, "fetchStorage"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleChangeOrg", [_dec6], Object.getOwnPropertyDescriptor(_class2.prototype, "handleChangeOrg"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleSelectStorage", [_dec7], Object.getOwnPropertyDescriptor(_class2.prototype, "handleSelectStorage"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleCancel", [_dec8], Object.getOwnPropertyDescriptor(_class2.prototype, "handleCancel"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleEdit", [_dec9], Object.getOwnPropertyDescriptor(_class2.prototype, "handleEdit"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleCreate", [_dec10], Object.getOwnPropertyDescriptor(_class2.prototype, "handleCreate"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleDelete", [_dec11], Object.getOwnPropertyDescriptor(_class2.prototype, "handleDelete"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleSaveStorage", [_dec12], Object.getOwnPropertyDescriptor(_class2.prototype, "handleSaveStorage"), _class2.prototype)), _class2)) || _class) || _class) || _class);
export { Storage as default };